package com.autoplaza;

import java.security.acl.LastOwnerException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MenuFragment extends Fragment {
	public int[] images;
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		
		images = new int[8];
		images[0] = R.drawable.m_inicio;
		images[1] = R.drawable.m_contacto;
		images[2] = R.drawable.m_faq;
		images[3] = R.drawable.m_sugerencias;
		images[4] = R.drawable.m_facebook;
		images[5] = R.drawable.m_twitter;

		
//		setListAdapter(new ArrayAdapter<String>(getActivity(),
//				android.R.layout.simple_list_item_1, new String[] { " First", " Second", " Third", " Fourth", " Fifth", " Sixth"}));
		
//		setListAdapter(new LazyAdapter(getActivity(), new String[] {"Inicio","Contacto","Preguntas Frecuentes","Sugerencias","Facebook","Twitter",}));

		
//		setListAdapter(new LazyAdapter(getActivity(), new String[] {"Inicio","Mi garage","Mis anuncios","Guía de precios","Noticias","Agencias y Lotes",
//			"Preguntas Frecuentes","Sugerencias"}));
		
//		getListView().setCacheColorHint(0);
	}

//	@Override
//	public void onListItemClick(ListView l, View v, int position, long id) {
//		super.onListItemClick(l, v, position, id);
//		((MenuActivity)getActivity()).getSlideoutHelper().close();
//		String url = "";
//		Intent i = new Intent(Intent.ACTION_VIEW);
//
//
//		switch (position) {
//		case 0:
//			
//			break;
//		case 1:
//			Intent goContacto = new Intent(getActivity(), ContactoFeedback.class);
//			startActivity(goContacto);
//			break;
//		case 2:
//			Intent goPreguntas = new Intent(getActivity(), PreguntasFrecuentes.class);
//			startActivity(goPreguntas);
//			break;
//		case 3:
//			Intent goSugerencias = new Intent(getActivity(), ContactoFeedback.class);
//			startActivity(goSugerencias);
//			break;
//		case 4:
//			url = "https://www.facebook.com/AutoplazaMexico?fref=ts";
//			i.setData(Uri.parse(url));
//			startActivity(i);
//			break;
//		case 5:
//			url = "https://twitter.com/AutoplazaMexico";
//			i.setData(Uri.parse(url));
//			startActivity(i);
//			break;
//
//		default:
//			break;
//		}
//		
//		
//		
//	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View myFragmentView = inflater.inflate(R.layout.fragment_menu_lateral, container, false);
		ListView lista = (ListView) myFragmentView.findViewById(R.id.lista_fragment_menu);
		lista.setAdapter(new LazyAdapter(getActivity(), new String[] {"Inicio","Contacto","Preguntas Frecuentes","Sugerencias","Facebook","Twitter",}));
		lista.setCacheColorHint(0);
		lista.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				String url = "";
				Intent i = new Intent(Intent.ACTION_VIEW);
				((MenuActivity)getActivity()).getSlideoutHelper().close();


				switch (position) {
				case 0:

					break;
				case 1:
					Intent goContacto = new Intent(getActivity(), ContactoFeedback.class);
					startActivity(goContacto);
					break;
				case 2:
					Intent goPreguntas = new Intent(getActivity(), PreguntasFrecuentes.class);
					startActivity(goPreguntas);
					break;
				case 3:
					Intent goSugerencias = new Intent(getActivity(), ContactoFeedback.class);
					startActivity(goSugerencias);
					break;
				case 4:
					Intent gourl = new Intent(getActivity(), Navegador.class);
					gourl.putExtra("url", "https://www.facebook.com/AutoplazaMexico?fref=ts");
					startActivity(gourl);
					
//					url = "https://www.facebook.com/AutoplazaMexico?fref=ts";
//					i.setData(Uri.parse(url));
//					startActivity(i);
					break;
				case 5:
					Intent gourl2 = new Intent(getActivity(), Navegador.class);
					gourl2.putExtra("url", "https://twitter.com/AutoplazaMexico");
					startActivity(gourl2);
//					url = "https://twitter.com/AutoplazaMexico";
//					i.setData(Uri.parse(url));
//					startActivity(i);
					break;

				default:
					break;
				}

			}
		});
		  
		
		 
		  return myFragmentView;
		
//		return super.onCreateView(inflater, container, savedInstanceState);
	}
	
	
	
	public class LazyAdapter extends BaseAdapter {
	    
	    private Activity activity;
	    private String[] data;
//	    private ImageView image;
	    private  LayoutInflater inflater=null;
//	    public ImageLoader imageLoader; 
	    
	    public LazyAdapter(Activity a, String[] d) {
	        activity = a;
	        data=d;
	        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//	        imageLoader=new ImageLoader(activity.getApplicationContext());
	    }

	    public int getCount() {
	        return data.length;
	    }

	    public Object getItem(int position) {
	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }
	    
	    public View getView(int position, View convertView, ViewGroup parent) {
	        View vi=convertView;
	        if(convertView==null)
	            vi = inflater.inflate(R.layout.item, null);

	        int size = Utils.getScreenSize2(getActivity());
	        if(size == 1){
	        	TextView text=(TextView)vi.findViewById(R.id.text);;
	 	        ImageView image=(ImageView)vi.findViewById(R.id.image);
	 	        image.setLayoutParams(new LinearLayout.LayoutParams(30,30));
	 	        text.setText(""+data[position]);
	 	        image.setImageResource(images[position]);	
	        }
	        if(size == 2){
	        	TextView text=(TextView)vi.findViewById(R.id.text);;
	 	        ImageView image=(ImageView)vi.findViewById(R.id.image);
	 	        image.setLayoutParams(new LinearLayout.LayoutParams(80,80));
	 	        text.setText(""+data[position]);
	 	        image.setImageResource(images[position]);	
	        }
	        if(size == 3){
	        	TextView text=(TextView)vi.findViewById(R.id.text);;
	 	        ImageView image=(ImageView)vi.findViewById(R.id.image);
	 	        image.setLayoutParams(new LinearLayout.LayoutParams(100,100));
	 	        text.setText(""+data[position]);
	 	        image.setImageResource(images[position]);
	        }
	        
	       
//	        ImageView im = vi.findViewById(R.id.)
//	        imageLoader.DisplayImage(data[position], image);
	        return vi;
	    }
	}

	
}
